home *** CD-ROM | disk | FTP | other *** search
- Path: vixen.cso.uiuc.edu!e-torres
- From: e-torres@students.uiuc.edu (Edgar Luis Torres)
- Newsgroups: comp.lang.c++
- Subject: HELP: Overloading [] operator for multidimensional array of objects.
- Date: 25 Mar 1996 16:17:53 GMT
- Organization: University of Illinois at Urbana
- Message-ID: <4j6gvh$3n4@vixen.cso.uiuc.edu>
- NNTP-Posting-Host: ux5.cso.uiuc.edu
- Summary: How does one specify overloading the []operator for array of objects?
- Keywords: overload, subscript, multidimensional, arrays, operator
-
- Hi!
-
- I would like to specify the overload of [], such that I may access
- a multidimensional array of objects. As an example, given I have a class
- EXAMCLASS and I wish to dynamically allocate a three-dimensional array of
- objects:
- EXAMCLASS object1***;
- ...
- object1 = malloc( sizeof(object1), dim1sz * dim2sz * dim3sz );
- I know the above is sacrilege to C++ purists, but bare with me, since the malloc
- does work correctly in C++. Now, what I want is to be able to:
- object1[arg1][arg2][arg3].obj1Var1 = something;
- OR
- something = object1[arg1][arg2][arg3].obj1Var1;
- In general, I know I have to include three friend operator in the definition of
- class EXAMCLASS, and then define them outside:
- EXAMCLASS** operator[](EXAMCLASS*** C, int i){
- return ((EXAMCLASS **)(C + (i * dim2sz * dim3sz))); }
- EXAMCLASS* operator[](EXAMCLASS** C, int i){
- return ((EXAMCLASS *)(C + (i * dim3sz))); }
- EXAMCLASS operator[](EXAMCLASS* C, int i){
- return *(C + i); }
- I know that the above does not work, but it does present the basic idea of what
- I am trying to do. If anybody has the knowledge to help with the specifics of
- syntax and argument rules, please HELP ME! Somebody suggested replacing the *
- with &, but I am not sure what EXAMCLASS&& means??
-
- Thanks in advance,
- Edgar
-
-
- --
- *()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*
- { Edgar Luis Torres Silva Electrical and Computer Engineering Dept }
- { 1912A Orchard Street Masters Graduate Student - Computer Eng }
- { Urbana, IL 61801 University of Illinois at Urbana-Champaign }
-